package com.useeinfo.oa.modules.base.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.security.entity.SystemUser;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @Auther: xudong.sun
 * @Date: 2018/11/30 10:09
 * @Description: 区域表
 */
@Entity
@Table(name = "area")
public class Area extends SuperClassEntity<Area> {

    /**
     * 区域名称
     */
    private String areaName;
    /**
     * 区域编号（自动生成）
     */
    private String areaCode;

    /**
     * 所有上级id，使用逗号分隔
     */
    @Column(length = 500)
    private String parentIds;

    /**
     * 组织左
     */
    private Integer lft;
    /**
     * 组织右
     */
    private Integer rgt;
    /**
     * 等级
     */
    private Integer lvl;
    /**
     * 上级区域
     */
    private Long parentId;
    /**
     * 最上级区域（不可能为空，如果本身是最上级 则设成本身的ID）
     */
    private Long rootId;
    /**
     * 关联企业
     * */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    /**
     * 描述
     */
    private String description;

    @ManyToMany(mappedBy = "areaSet")
    private Set<Personnel>personnelSet = new HashSet<>();

    @ManyToMany(mappedBy = "areaSet")
    private Set<SystemUser>systemUserSet = new HashSet<>();

    /**
     * 创建人ID
     */
    private Long systemUserId;
    /**
     * 创建人姓名
     */
    private String systemUserName;

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getLft() {
        return lft;
    }

    public void setLft(Integer lft) {
        this.lft = lft;
    }

    public Integer getRgt() {
        return rgt;
    }

    public void setRgt(Integer rgt) {
        this.rgt = rgt;
    }

    public Integer getLvl() {
        return lvl;
    }

    public void setLvl(Integer lvl) {
        this.lvl = lvl;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public Set<Personnel> getPersonnelSet() {
        return personnelSet;
    }

    public void setPersonnelSet(Set<Personnel> personnelSet) {
        this.personnelSet = personnelSet;
    }

    public Set<SystemUser> getSystemUserSet() {
        return systemUserSet;
    }

    public void setSystemUserSet(Set<SystemUser> systemUserSet) {
        this.systemUserSet = systemUserSet;
    }

    public Long getSystemUserId() {
        return systemUserId;
    }

    public void setSystemUserId(Long systemUserId) {
        this.systemUserId = systemUserId;
    }

    public String getSystemUserName() {
        return systemUserName;
    }

    public void setSystemUserName(String systemUserName) {
        this.systemUserName = systemUserName;
    }

    public Long getRootId() {
        return rootId;
    }

    public void setRootId(Long rootId) {
        this.rootId = rootId;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("areaName", StringConverters.null2String(getAreaName()));
        jsonObject.put("areaCode", StringConverters.null2String(getAreaCode()));
        jsonObject.put("lvl", getLvl());
        jsonObject.put("parentId", getParentId());
        jsonObject.put("systemUserId", CommonSugar.getTypedDefault(getSystemUserId(), 0L));
        jsonObject.put("systemUserName", StringConverters.null2String(getSystemUserName()));
        if(getEnterprise()!=null){
            jsonObject.put("enterpriseName", StringConverters.null2String(getEnterprise().getEnterpriseName()));
        }else{
            jsonObject.put("enterpriseName", "");
        }

        return jsonObject;
    }
}
